﻿using Common.Communal;
using ERP.Domain.Scheduling;
using Scheduling.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scheduling.Services.Teams
{
    public class TeamServices : ITeamServices
    {
        public TeamServices(ITeamRepository team)
        {
            Team = team;
        }

        public ITeamRepository Team { get; }

        public async Task<ApiResult<int>> AddTeam(Team team)
        {
            ApiResult<int> result = new ApiResult<int>();
            
            try
            {
                // 非空判断
                if (team == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组信息不能为空";
                    return result;
                }

                // 必填字段验证
                if (string.IsNullOrWhiteSpace(team.TeamName))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组名称不能为空";
                    return result;
                }

                if (team.TeamTypeId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型ID不能为空或无效";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(team.TeamLeader))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组组长不能为空";
                    return result;
                }

                result.Code = EnumCode.Ok;
                result.Message = "班组添加成功";
                var list = await Team.AddAsync(team);
                result.Data = list;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"添加班组失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> UpdTeam(Team team)
        {
            ApiResult<int> result = new ApiResult<int>();
            
            try
            {
                // 非空判断
                if (team == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组信息不能为空";
                    return result;
                }

                // 必填字段验证
                if (team.TeamId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组ID不能为空或无效";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(team.TeamName))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组名称不能为空";
                    return result;
                }

                if (team.TeamTypeId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型ID不能为空或无效";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(team.TeamLeader))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组组长不能为空";
                    return result;
                }

                // 检查班组是否存在
                var existingTeam = await Team.GetModelAsync(team.TeamId);
                if (existingTeam == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "要修改的班组不存在";
                    return result;
                }

                // 使用已跟踪的实体进行更新，避免重复跟踪同一主键导致的异常
                existingTeam.TeamName = team.TeamName;
                existingTeam.TeamTypeId = team.TeamTypeId;
                existingTeam.TeamLeader = team.TeamLeader;
                existingTeam.TeamStatus = team.TeamStatus;

                result.Code = EnumCode.Ok;
                result.Message = "班组修改成功";
                var list = await Team.Update(existingTeam);
                result.Data = list;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"修改班组失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> DeleteTeam(int teamId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                // 根据ID获取班组信息
                var team = await Team.GetModelAsync(teamId);
                if (team == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组不存在";
                    return result;
                }

                // 将IsDelete状态设置为true
                team.IsDelete = true;
                
                // 更新班组信息
                var updateResult = await Team.Update(team);
                
                result.Code = EnumCode.Ok;
                result.Message = "班组删除成功";
                result.Data = updateResult;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"删除班组失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }
    }
}
